package Main;

import url.DBUtil;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.SQLException;

public class ReservationSystem extends JFrame {

    public ReservationSystem() {

        Font f = new Font("宋体", Font.PLAIN, 25);
        JPanel jPanel = new JPanel(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.insets = new Insets(10, 10, 20, 10);

        JLabel j1 = new JLabel("账  号");
        j1.setFont(f);
        gbc.gridx = 0;
        gbc.gridy = 0;
        jPanel.add(j1, gbc);
        JTextField jText0 = new JTextField(15);
        jText0.setFont(f);
        gbc.gridx = 1;
        jPanel.add(jText0, gbc);

        JLabel j2 = new JLabel("密  码");
        j2.setFont(f);
        gbc.gridx = 0;
        gbc.gridy = 1;
        jPanel.add(j2, gbc);
        JPasswordField jText1 = new JPasswordField(15);
        jText1.setFont(f);
        gbc.gridx = 1;
        jPanel.add(jText1, gbc);

        JButton jb0 = new JButton("登陆");
        JButton jb1 = new JButton("注册");
        jb0.setFont(f);
        jb1.setFont(f);
        gbc.gridx = 0;
        gbc.gridy = 2;
        jPanel.add(jb0, gbc);
        gbc.gridx = 1;
        jPanel.add(jb1, gbc);

        jPanel.setOpaque(false);
        this.add(jPanel, BorderLayout.CENTER); // Remove unnecessary semicolon
        background();

        // 设置字体大小
        setFont(new Font("宋体", Font.PLAIN, 25));
        // 窗口大小
        setSize(700, 700);
        // 窗口可视化
        setVisible(true);
        // 窗口标题
        setTitle("场地管理系统");
        // 窗口初始化位置
        setLocationRelativeTo(null);
        // 窗口大小不可改变
        setResizable(false);
        // 窗口的关闭事件
        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });

        jb0.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                DBUtil.initst();
                String text = jText0.getText();
                char[] passwordChars = jText1.getPassword();
                String password = new String(passwordChars);
                String sql = "select * from account where username = '" + text + "'";
                DBUtil.select(sql);
                boolean flag1 = false;
                boolean flag2 = false;

                while (true) {
                    try {
                        while(DBUtil.rs.next()){
                            flag1 = true;
                            if(DBUtil.rs.getString(2).equals(password)){
                                flag2 = true;
                                new MainFrame();
                                break;
                            }
                        }
                        if(flag1 && flag2){
                            dispose();
                            break;
                        }
                        if(!flag1){
                            JOptionPane.showMessageDialog(null, "用户不存在");
                            break;
                        }
                        if(!flag2){
                            JOptionPane.showMessageDialog(null, "密码错误");
                            break;
                        }
                    } catch (SQLException ex) {
                        throw new RuntimeException(ex);
                    }
                }
                DBUtil.closeDB();;


            }
        });

        jb1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                new RegisterDialog(); // 实例化对话框
            }
        });
    }

    public void background() {
        ((JPanel) this.getContentPane()).setOpaque(false);
        ImageIcon img = new ImageIcon("src/img/1.jpg"); //添加图片
        JLabel background = new JLabel(img);
        this.getLayeredPane().add(background, new Integer(Integer.MIN_VALUE));
        background.setBounds(0, 0, img.getIconWidth(), img.getIconHeight());
    }

    public static void main(String[] args) {
        new ReservationSystem();
    }
}
